﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pipes.Data;
using System.IO;

namespace Pipes.Xml
{
  public class XmlTextReader : IXmlReader, IPipeInput<System.IO.TextReader>
  {
    private System.Xml.WhitespaceHandling _whitespaceHandling = System.Xml.WhitespaceHandling.Significant;
    protected IEnumerable<System.IO.TextReader> _source;

    public System.Xml.WhitespaceHandling WhitespaceHandling
    {
      get
      {
        return _whitespaceHandling;
      }
      set
      {
        _whitespaceHandling = value;
      }
    }

    public XmlTextReader() { }

    public void Initialize(IEnumerable<System.IO.TextReader> source)
    {
      _source = source;
    }

    public virtual IEnumerable<System.Xml.XmlReader> GetReaders()
    {
      foreach (var textReader in _source)
      {
        yield return new System.Xml.XmlTextReader(textReader) { WhitespaceHandling = _whitespaceHandling };
      }
    }
    public IEnumerator<IXmlNode> GetEnumerator()
    {
      foreach (var reader in GetReaders())
      {
        while (reader.Read())
        {
          yield return Util.GetNode(reader);
        }
      }
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      return GetEnumerator();
    }
  }
}
